/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Banking;
import entity.Hospital;
import entity.Payment;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import sessionBeans.PaymentFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class PaymentManagement implements Serializable {

    @EJB
    private PaymentFacadeLocal paymentFacade;
    private Payment payment = new Payment();
    private Payment selectPayment = new Payment();
    private List<Payment> list = null;
    private List<Payment> filteredList = null;
    private Hospital hospital = new Hospital();
    private Banking banking = new Banking();

    public void EditPayment() {
        hospital = selectPayment.getHospital();
        banking = selectPayment.getBanking();
    }

    public void create() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            payment.setPaymentKey(getBanking().getBankId().toString() + getHospital().getHospitalId().toString());
            payment.setBanking(getBanking());
            payment.setHospital(getHospital());

            paymentFacade.create(payment);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create ", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage() + " Duplicate Name");
        }
        banking = new Banking();
        hospital = new Hospital();

        payment = new Payment();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            selectPayment.setBanking(banking);
            selectPayment.setHospital(hospital);
            paymentFacade.edit(selectPayment);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit ", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    public void delete() {

        FacesMessage msg = null;
        try {

            paymentFacade.remove(selectPayment);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Delere", "Delete Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);

    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public Payment getSelectPayment() {
        return selectPayment;
    }

    public void setSelectPayment(Payment selectPayment) {
        this.selectPayment = selectPayment;
    }

    public List<Payment> getList() {
        list = paymentFacade.findAll();
        return list;
    }

    public void setList(List<Payment> list) {
        this.list = list;
    }

    public List<Payment> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Payment> filteredList) {
        this.filteredList = filteredList;
    }

    public Hospital getHospital() {
        return hospital;
    }

    public void setHospital(Hospital hospital) {
        this.hospital = hospital;
    }

    public Banking getBanking() {
        return banking;
    }

    public void setBanking(Banking banking) {
        this.banking = banking;
    }
   
}
